package com.sudichina.sudichina.view.customview.xlistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.common.reflect.TypeToken;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Car;
import com.sudichina.sudichina.bean.Line;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.querydatabase.DBManager;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ItemAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<Line> datas;
    private OnDeleteClickListener listener;
    private String toZoneCode;


    public ItemAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public void setData(List<Line> datas) {
        this.datas = datas;
    }

    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    public Object getItem(int position) {
        return datas.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder(convertView);
//            holder.coating = (TextView) convertView.findViewById(R.id.tv_coating);
//            holder.functions = (TextView) convertView.findViewById(R.id.tv_functions);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

//        holder.list_item_textview.setText("");


//        holder.coating.setVisibility(View.VISIBLE);
//        holder.functions.setClickable(false);
// TODO: 2017/3/24 0024
//        holder.ivItemLinemanage.setImageDrawable(itemWayManagerBean.icon);
        final Line line = datas.get(position);
        //用户名
        holder.tvItemLmaUsername.setText(MyApp.getCurrentUser().getAccountName());
        //车牌号
        //http://192.168.1.37:8888/services/car/loadCar/{id}

        getChePai(holder, line);


        holder.tvItemLmaFrom.setText(line.getFromAddress());
        //到达区域
        //解析数据库 把到达区域解析出来
        DBManager dbManager= new DBManager(MyApp.getContext());

        toZoneCode = line.getToZoneCode();


        if(toZoneCode!=null){

            String name = dbManager.getCityFromCitycode(toZoneCode);
            holder.tvItemLmaTo.setText( name);

        }else {
            holder.tvItemLmaTo.setText( "未知");
        }



        holder.llItemlineDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                listener.onDeleteClick(position, line, ItemAdapter.this);

            }
        });

        return convertView;
    }


    private void getChePai(ViewHolder holder, Line line) {
        HashMap<String, Object> hm = new HashMap<>();
        hm.put("id", line.getDicId());
        final ViewHolder finalHolder = holder;
        //  java.lang.String cannot be cast to com.sudichina.sudichina.bean.Result
        Type type = new TypeToken<Result<Car>>() {
        }.getType();
        NetUtils.requestData(Api.LOADCAR + line.getCarId(), hm, new onRequestDataFinish() {
            @Override
            public void requestdataFinish(String catalog, Object bean) {
                try {
                    Result<Car> bean1 = (Result<Car>) bean;
                    finalHolder.tvItemLmaPlatenumber.setText(bean1.getCode() == 1 ? bean1.getData().getCarFirstLetter() + bean1.getData().getCardNumber() : "");
                } catch (Exception e) {

                }


            }

            @Override
            public void requestdataFailed(IOException e) {

            }
        }, type, false);
    }


    public void relpaceData(List<Line> data) {
        this.datas = data;
    }


    public static class ViewHolder {

        //        public TextView coating;
//        public TextView functions;
        @BindView(R.id.ll_itemline_delete)
        LinearLayout llItemlineDelete;
        @BindView(R.id.iv_item_linemanage)
        ImageView ivItemLinemanage;
        @BindView(R.id.tv_item_lma_username)
        TextView tvItemLmaUsername;
        //车牌号
        @BindView(R.id.tv_item_lma_platenumber)
        TextView tvItemLmaPlatenumber;
        @BindView(R.id.tv_item_lma_from)
        TextView tvItemLmaFrom;
        @BindView(R.id.tv_item_lma_to)
        TextView tvItemLmaTo;
        //        @BindView(R.id.tv_functions)
//        TextView tvFunctions;
//        @BindView(R.id.tv_coating)
//        TextView tvCoating;
        @BindView(R.id.ll_parent)
        RelativeLayout llParent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void setOnDeleteClickListener(OnDeleteClickListener listener) {
        this.listener = listener;
    }

    public interface OnDeleteClickListener {

        void onDeleteClick(int position, Line line, ItemAdapter itemAdapter);
    }

}
